package tt.dz.mvc.controller.base;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.exception.BizException;

public abstract class BaseController
{
	private Log log = LogFactory.getLog(BaseController.class);

	@ExceptionHandler
	@ResponseBody
	public JsonCommon exp(Exception ex)
	{
		log.error("Exception is caught by handler", ex);
		if (ex instanceof BizException)
		{
			BizException b = (BizException) ex;
			if (StringUtils.isNotEmpty(b.getErrorCode()))
				return new JsonCommon<>(b.getErrorCode(), ex.getMessage());
		}

		String msg = ex.getMessage();
		if (StringUtils.isEmpty(msg))
			msg = ex.getClass().getName();

		return new JsonCommon<>("400", msg);

	}
}
